package edu.gatech.cs2340.sharks.fms.data;

/**
 * UserAdapter.java
 * @author Wai T. Yeung
 * 
 * Used to adapt an array of Users to a format for displaying
 */

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import edu.gatech.cs2340.sharks.fms.R;

public class UserAdapter extends ArrayAdapter<User>
{
	/**
	 * Context object containing the current context of the application. 
	 */
	private Context context;
	
	/**
	 * Integer containing the ID of the resource containing the layout for a row in the listview. 
	 */
	private int layoutResourceID;
	
	/**
	 * An array of Users containing the data that will populate the listview.  
	 */
	private User[] data = null;

	/**
	 * Constructor for the UserAdapter.
	 * @param context the current context of the application
	 * @param layoutResourceID the ID of the resource containing the layout for a row in the listview
	 * @param data an array of Users that will populate the listview.
	 */
	public UserAdapter(Context context, int layoutResourceID, User[] data)
	{
		super(context, layoutResourceID, data);
		this.context = context;
		this.layoutResourceID = layoutResourceID;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		UserHolder holder;
		
		if(row == null)
		{
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			row = inflater.inflate(layoutResourceID, parent, false);
			
			holder = new UserHolder();
			holder.textViewUsername = (TextView)row.findViewById(R.id.textViewUsername);
			holder.textViewLockStatus = (TextView)row.findViewById(R.id.textViewLockStatus);
			holder.buttonViewUser = (Button)row.findViewById(R.id.buttonViewUser);
			
			row.setTag(holder);
		}
		else holder = (UserHolder)row.getTag();
		
		User user = data[position];
		holder.textViewUsername.setText(user.handle());
		if(user.valid) holder.textViewLockStatus.setText("Unlocked");
		else holder.textViewLockStatus.setText("Locked");
		
		holder.buttonViewUser.setText("View User");
		holder.buttonViewUser.setTag(user.handle());

		return row;
	}

	/**
	 * Information holder class
	 */
	static class UserHolder
	{
		TextView textViewUsername;
		TextView textViewLockStatus;
		Button buttonViewUser;
	}
}